package cn.com.kblog.controller.admin;


import java.io.File;
import java.util.HashMap;
import java.util.Map;

import cn.com.kblog.common.Constant;
import cn.com.kblog.common.Result;
import cn.com.kblog.model.EmailVerify;
import cn.com.kblog.model.User;
import cn.com.kblog.util.AppConfigUtil;
import cn.com.kblog.util.LoginUtils;

import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.Page;

@ControllerBind(controllerKey = "/admin/user",viewPath="/WEB-INF/pages/admin/user")
public class UserController extends Controller{
	/**
	 * 分页查询user
	 */
	public void list(){
		Integer pageNumber = getParaToInt(0,Constant.PAGE_NUM_DEFAULT);
		Integer pageSize = getParaToInt("pageSize", Constant.PAGE_SIZE_DEFAULT);
		String orderField = getPara("orderField", Constant.PAGE_ORDER_FIELD_DEFAULT);
		String orderDirection = getPara("orderDirection", Constant.PAGE_ORDER_DIRECTION_DEFAULT);
		Page<User> beans = User.dao.pageQuery(pageNumber, pageSize, orderField, orderDirection, null);
		setAttr(Constant.BEANS_KEY, beans);
		render("user_list.html");
	}
	/**
	 * 添加user
	 */
	public void add(){
		String id = getPara(0);
		if (StrKit.isBlank(id)) {
		    render("user_add.html");
		    return;
		}
		User bean = User.dao.findById(id);
		if (bean == null) {
			render("user_add.html");
		    return;
		}
		setAttr(Constant.BEAN_KEY, bean);
		render("user_add.html");
	}
	/**
	 * 修改user
	 */
	public void update(){
		String id = getPara(0);
		//id为空则是修改当前登录人
		if (StrKit.isBlank(id)) {
			setAttr(Constant.BEAN_KEY, LoginUtils.getLoginUser());
			render("user_update.html");
		    return;
		}
		User bean = User.dao.findById(id);
		if (bean == null) {
			setAttr(Constant.RESULT_KEY, "信息错误");
			render("user_list.html");
		    return;
		}
		setAttr(Constant.BEAN_KEY, bean);
		render("user_update.html");
	}
	/**
	 * 保存user
	 */
	public void save(){
		User bean = getModel(User.class,Constant.BEAN_KEY);
		if(bean==null){
			keepPara();
			Result result =new Result(false, "参数错误!");
			setAttr(Constant.RESULT_KEY, result);
			render("user_add.html");
			return;
		}
		Integer userId = bean.getID();
		if(userId==null){
			//添加用户需要检测邮箱是否已存在
			User user = User.dao.findByEmail(bean.getStr(User.EMAIL));
			if(user!=null){
				keepPara();
				Result result =new Result(false, "邮箱已存在!");
				setAttr(Constant.RESULT_KEY, result);
				render("user_add.html");
				return;
			}
		}
		//保存
		Result result = bean.saveOrUpdate();
		//添加用户需要发送验证邮件
		if(userId==null){
			String subject = "K博客验证邮件";
			String to = bean.get(User.EMAIL);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("domain", AppConfigUtil.get("domain.name"));
			params.put(User.NICK_NAME, bean.getStr(User.NICK_NAME));
			params.put("verify_code", EmailVerify.dao.createVerify(bean.getID()));
			String dir = PathKit.getWebRootPath() + File.separator + "WEB-INF" + File.separator + "template";
			String tempname = "email_verify.ftl";
			//TODO 暂时去掉
			//SendHtmlEmail4BAE.sendTemplateEmail(subject, to, params,dir, tempname);
			
		}
		setAttr(Constant.RESULT_KEY, result);
		list();
	}
	
	/**
	 * 再次发送邮件验证
	 */
	public void send_email_verify(){
		String email = getPara("email");
		User user = User.dao.findByEmail(email);
		if(user==null){
			renderJson(new Result(false,"信息错误！"));
			
		}
		
		String subject = "K博客验证邮件";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("domain", AppConfigUtil.get("domain.name"));
		params.put(User.NICK_NAME, email);
		params.put("verify_code", EmailVerify.dao.createVerify(user.getID()));
		String dir = PathKit.getWebRootPath() + File.separator + "WEB-INF" + File.separator + "template";
		String tempname = "email_verify.ftl";
		//TODO
		//SendHtmlEmail4BAE.sendTemplateEmail(subject, email, params,dir, tempname);
		
		renderJson(new Result(true,"发送成功！"));
	}
	
	/**
	 * 删除user
	 */
	public void delete(){
		String id = getPara(0);
		if (StrKit.isBlank(id)) {
			renderJson(new Result(false, "信息错误！"));
		    render("user_list.html");
		    return;
		}
		Result result = User.dao.deleteById(id);
		renderJson(result);
	}
	
	/**
	 * 重置密码
	 */
	public void reset_password(){
		render("reset_password.html");
	}
	/**
	 * 当前登录账号修改密码
	 */
	public void password_save(){
		String password =  getPara("password");
		User user = LoginUtils.getLoginUser();
		user.set(User.PASSWORD, password);
		setAttr(Constant.RESULT_KEY, user.saveOrUpdate());
		list();
	}
}
